﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlexiGridHelper;
using System.Data;
using System.Web.Script.Serialization;

namespace Golf.WebUI.HttpHandler.RebateLogList
{
    /// <summary>
    /// RebateLogList 的摘要说明
    /// </summary>
    public class RebateLogList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer js = new JavaScriptSerializer();
            Golf.BLL.Golf_RebateLog bll = new Golf.BLL.Golf_RebateLog();
            List<Golf.Model.Golf_RebateLog> list = new List<Golf.Model.Golf_RebateLog>();
            //实例化页面信息类，获取页面传来的参数。该类是FlexiGridHelper中定义的类，封装了很多功能需要的数据
            PageView pageView = new PageView(context);
            //获取分页后的数据集
            DataSet ds = bll.GetList(pageView.PageSize, pageView.PageIndex, "");
            //构建表格
            DataTable dt = new DataTable();
            //构建列
            //注意：第一列必须是数据库的主键列
            DataColumn dc0 = new DataColumn("Id", typeof(string));
            DataColumn dc1 = new DataColumn("UserId", typeof(string));
            DataColumn dc2 = new DataColumn("OrderId", typeof(string));
            DataColumn dc3 = new DataColumn("OrderNumber", typeof(string));
            DataColumn dc4 = new DataColumn("RebateType", typeof(string));
            DataColumn dc5 = new DataColumn("CouponsNumber", typeof(string));
            DataColumn dc6 = new DataColumn("Remark", typeof(string));
            DataColumn dc7 = new DataColumn("CreateTime", typeof(string));
            //把构建的列添加到数据表中
            dt.Columns.AddRange(new DataColumn[] { dc0, dc1, dc2, dc3, dc4, dc5, dc6, dc7 });//这里有个顺序的哟 
            list = bll.DataTableToList(ds.Tables[0]);//获取第一张表的数据  就是数据库中的数据
            //填充数据
            foreach (var item in list)
            {
                //新建行，注意是dt.NewRow();而不是new DataRow();
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                dr["Id"] = item.Id;
                dr["UserId"] = item.UserId;
                dr["OrderId"] = item.OrderId;
                dr["OrderNumber"] = item.OrderNumber;
                dr["RebateType"] = item.RebateType;
                dr["CouponsNumber"] = item.CouponsNumber;
                dr["Remark"] = item.Remark;
                dr["CreateTime"] = item.CreateTime;
            }
            //调用FlexiGrid数据格式转换方法
         
            context.Response.Write(js.Serialize(new GetDataHelper().GetFlexiGridData(pageView, dt, (int)ds.Tables[1].Rows[0][0])));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}